package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
public class WebSkuDetailController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor myExecutor;
    @Autowired
    private SearchFeignClient searchFeignClient;

    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model) {
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1.根据商品skuId查询商品基本信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            model.addAttribute("skuInfo",skuInfo);
            return skuInfo;
        },myExecutor);

        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            //2.根据三级分类id查询商品分类信息
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            model.addAttribute("categoryView", categoryView);
        },myExecutor);

        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            //3.根据skuId查询商品实时价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            model.addAttribute("price", skuPrice);
        },myExecutor);

        //商品热度加一
        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
            searchFeignClient.incrHotScore(skuId);
        },myExecutor);

        CompletableFuture<Void> salePropertyFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            //4.销售属性组合与skuId的对应关系
            Long productId = skuInfo.getProductId();
            Map<Object, Object> mapping = productFeignClient.getSalePropertyAndSkuIdMapping(productId);
            model.addAttribute("salePropertyValueIdJson", JSON.toJSONString(mapping));
        },myExecutor);

        CompletableFuture<Void> spuSalePropertyFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Long productId = skuInfo.getProductId();
            //5.该sku对应的销售属性（一份）所有的销售属性（全份）
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSalePropertyAndSelected(productId, skuId);
            model.addAttribute("spuSalePropertyList", spuSalePropertyList);
        },myExecutor);
        //所有任务执行完后返回结果
        CompletableFuture.allOf(skuInfoFuture,priceFuture,categoryViewFuture,
                                salePropertyFuture,spuSalePropertyFuture,hotScoreFuture).join();

        return "detail/index";
    }

}
